﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Activity.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The activity.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Services.Dto
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;

    /// <summary>
    /// The activity.
    /// </summary>
    [DataContract]
    public class Activity
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the attendees.
        /// </summary>
        [DataMember]
        public List<Guid> Attendees { get; set; }

        /// <summary>
        /// Gets or sets the bda.
        /// </summary>
        [DataMember]
        public BusinessDevelopmentActivity BDA { get; set; }

        /// <summary>
        /// Gets or sets the commentary.
        /// </summary>
        [DataMember]
        public string Commentary { get; set; }

        /// <summary>
        /// Gets or sets the creator.
        /// </summary>
        [DataMember]
        public Guid? Creator { get; set; }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        [DataMember]
        public Guid ID { get; set; }

        /// <summary>
        /// Gets or sets the location.
        /// </summary>
        [DataMember]
        public Guid? Location { get; set; }

        /// <summary>
        /// Gets or sets the na.
        /// </summary>
        [DataMember]
        public NetworkingActivity NA { get; set; }

        /// <summary>
        /// Gets or sets the owner projects.
        /// </summary>
        [DataMember]
        public List<Guid> OwnerProjects { get; set; }

        /// <summary>
        /// Gets or sets the time of activity.
        /// </summary>
        [DataMember]
        public DateTime? TimeOfActivity { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The convert.
        /// </summary>
        /// <param name="activity">
        /// The activity.
        /// </param>
        /// <returns>
        /// The <see cref="Activity"/>.
        /// </returns>
        public static Activity Convert(Models.Domain.Activity activity)
        {
            if (activity != null)
            {
                var dto = new Activity
                              {
                                  ID = activity.ID, 
                                  Commentary = activity.Commentary, 
                                  TimeOfActivity = activity.TimeOfActivity, 
                                  Location = (activity.Location ?? new Models.Domain.Location()).ID, 
                              };
                if (activity.OwnerProjects != null && activity.OwnerProjects.Any())
                {
                    dto.OwnerProjects = new List<Guid>();
                    foreach (Models.Domain.Project proj in activity.OwnerProjects)
                    {
                        dto.OwnerProjects.Add(proj.ID);
                    }
                }

                if (activity.NA != null)
                {
                    var na = new NetworkingActivity
                                 {
                                     ID = activity.NA.ID, 
                                     Attendance = activity.NA.Attendance, 
                                     Booth = activity.NA.Booth, 
                                     Organizer = activity.NA.Organizer,
                                     ProductType = activity.NA.ProductType, 
                                     PresentationComment = activity.NA.PresentationComment,
                                     IsConferenceEvent = activity.NA.IsConferenceEvent,
                                     IsDelegationTravel = activity.NA.IsDelegationTravel,
                                     IsNetworkingEvent = activity.NA.IsNetworkingEvent,
                                     IsProductLaunch = activity.NA.IsProductLaunch,
                                     IsNational = activity.NA.IsNational
                                 };
                  
                    if (activity.NA.Presentations != null)
                    {
                        na.Presentations = new List<Guid>();
                        foreach (Models.Domain.Presentation pres in activity.NA.Presentations)
                        {
                            na.Presentations.Add(pres.ID);
                        }
                    }

                    if (activity.NA.SupportForBusinessAttendance != null)
                    {
                        na.SupportForBusinessAttendance = new List<Guid>();
                        foreach (Models.Domain.Profile prof in activity.NA.SupportForBusinessAttendance)
                        {
                            na.SupportForBusinessAttendance.Add(prof.ID);
                        }
                    }

                    dto.NA = na;
                }

                if (activity.BDA != null)
                {
                    var bda = new BusinessDevelopmentActivity
                                  {
                                      ID = activity.BDA.ID, 
                                      AdditionalComments = activity.BDA.AdditionalComments, 
                                      TopicElaboration = activity.BDA.TopicElaboration
                                  };
                    if (activity.BDA.Topics != null)
                    {
                        bda.Topics = new List<Guid>();
                        foreach (Models.Domain.Topic top in activity.BDA.Topics)
                        {
                            bda.Topics.Add(top.ID);
                        }
                    }

                    dto.BDA = bda;
                }

                if (activity.Creator != null)
                {
                    dto.Creator = activity.Creator.ID;
                }

                if (activity.Attendees != null)
                {
                    dto.Attendees = new List<Guid>();
                    foreach (Models.Domain.Profile prof in activity.Attendees)
                    {
                        dto.Attendees.Add(prof.ID);
                    }
                }

                return dto;
            }
            
            return null;
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Queries
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Activities;
    using ServiceLibrary.Services.Dto;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The activity query.
    /// </summary>
    [DataContract]
    public class ActivityQuery : IQuery
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the at location.
        /// </summary>
        [DataMember]
        public Guid? AtLocation { get; set; }

        /// <summary>
        /// Gets or sets the BA contains topics.
        /// </summary>
        [DataMember]
        public List<Guid> BAContainsTopics { get; set; }

        /// <summary>
        /// Gets or sets the default fields.
        /// </summary>
        [DataMember]
        public DefaultSearchFields DefaultFields { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether results should be business development.
        /// </summary>
        [DataMember]
        public bool HasBusinessDevelopment { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether results should be network activity.
        /// </summary>
        [DataMember]
        public bool HasNetworkActivity { get; set; }

        /// <summary>
        /// Gets or sets attendees.
        /// </summary>
        [DataMember]
        public List<Guid> WithAttendees { get; set; }

        /// <summary>
        /// Gets or sets the presentations.
        /// </summary>
        [DataMember]
        public List<Guid> WithPresentations { get; set; }

        /// <summary>
        /// Gets or sets the organizer.
        /// </summary>
        [DataMember]
        public Guid? WithOrganizer { get; set; }

        /// <summary>
        /// Gets or sets the profiles in support for business attendance.
        /// </summary>
        [DataMember]
        public List<Guid> WithSupportForBusinessAttendance { get; set; }

        /// <summary>
        /// Gets or sets whether its a networking event or not.
        /// </summary>
        [DataMember]
        public bool? IsNetworkingEvent { get; set; }

        /// <summary>
        /// Gets or sets whether its a conference event or not.
        /// </summary>
        [DataMember]
        public bool? IsConferenceEvent { get; set; }

        /// <summary>
        /// Gets or sets whether its a delegation travel event or not.
        /// </summary>
        [DataMember]
        public bool? IsDelegationTravel { get; set; }

        /// <summary>
        /// Gets or sets whether its a delegation travel event or not.
        /// </summary>
        [DataMember]
        public bool? IsProductLaunch { get; set; }

        /// <summary>
        /// Gets or sets whether WT was participating.
        /// </summary>
        [DataMember]
        public bool? NAAttendance { get; set; }

        /// <summary>
        /// Gets or sets whether WT had a booth.
        /// </summary>
        [DataMember]
        public bool? NABooth { get; set; }

        /// <summary>
        /// Gets or sets whether WT was organizing.
        /// </summary>
        [DataMember]
        public bool? NAOrganizer { get; set; }

        /// <summary>
        /// Gets or sets whether the profiles are at remote a area.
        /// </summary>
        [DataMember]
        public bool? ProfilesAreAtRemoteArea { get; set; }

        /// <summary>
        /// Gets or sets if it's an entrepreneur.
        /// </summary>
        [DataMember]
        public bool? ProfilesAreEntrepreneur { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <returns>
        /// The <see cref="IResult"/>.
        /// </returns>
        public override IResult Execute(ISession hsession)
        {
            var actioner = new ActivityActioner(hsession, null);
            return actioner.ExecuteSearchQuery(this);
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Results
{
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    using ServiceLibrary.Services.Dto;

    /// <summary>
    /// The activity result.
    /// </summary>
    [DataContract]
    public class ActivityResult : IResult
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ActivityResult"/> class.
        /// </summary>
        public ActivityResult()
        {
            this.List = new List<Activity>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the list.
        /// </summary>
        [DataMember]
        public List<Activity> List { get; set; }

        /// <summary>
        /// Gets or sets the total row count.
        /// </summary>
        [DataMember]
        public int TotalRowCount { get; set; }

        #endregion
    }
}

namespace ServiceLibrary.Services.Commands
{
    using System;
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Activities;
    using ServiceLibrary.Services.Dto;

    using User = ServiceLibrary.Models.Domain.User;

    /// <summary>
    /// The create activity command.
    /// </summary>
    [DataContract]
    public class CreateActivityCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the dto.
        /// </summary>
        [DataMember]
        public Activity Dto { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new ActivityActioner(hsession, activeUser);
            actioner.CreateActivity(this);
        }

        #endregion
    }

    /// <summary>
    /// The update activity command.
    /// </summary>
    [DataContract]
    public class UpdateActivityCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the dto.
        /// </summary>
        [DataMember]
        public Activity Dto { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new ActivityActioner(hsession, activeUser);
            actioner.UpdateActivity(this);
        }

        #endregion
    }

    /// <summary>
    /// The delete activity command.
    /// </summary>
    [DataContract]
    public class DeleteActivityCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the dto.
        /// </summary>
        [DataMember]
        public Guid? Dto { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new ActivityActioner(hsession, activeUser);
            actioner.DeleteActivity(this);
        }

        #endregion
    }
}